package com.my.study.atomic;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author Carlos
 * @version 1.0
 * @Description TODO
 * @date 2021/6/1 8:30
 **/
@Slf4j(topic = "c.LockCas")
public class LockCas {

    /**
     * 0：未加锁
     * 1：加锁
     */
    private AtomicInteger state = new AtomicInteger(0);


    public void lock() {
        log.debug("lock...");
        while (true) {
            if (state.compareAndSet(0, 1)) {
                break;
            }
        }
    }

    public void unLock() {
        log.debug("unLock...");
        state.set(0);
    }

}
